package com.ibeeking.found.content.b.rest.service.impl;

import com.ibeeking.found.content.b.rest.service.IBroadcastService;
import com.ibeeking.found.content.service.common.dos.BroadcastDO;
import com.ibeeking.found.content.service.common.vo.BroadcastCount;
import com.ibeeking.found.content.service.mapper.BroadcastMapper;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BroadcastServiceImpl
 * @Description 直播间
 * @Author ibeeking
 * @Date 2022-02-18 14:11
 **/
@Service
public class BroadcastServiceImpl extends ServiceImpl<BroadcastMapper, BroadcastDO> implements IBroadcastService {

    @Resource
    private BroadcastMapper broadcastMapper;

    /**
     * 统计关注数量
     *
     * @param memberIds
     * @return
     */
    @Override
    public List<BroadcastCount> countByAttention(List<Long> memberIds) {
        QueryWrapper<BroadcastDO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .in(Collections3.isNotEmpty(memberIds), "group_id", memberIds)
                .select("count(1) as num, group_id as id")
                .groupBy(" group_id");
        List<Map<String, Object>> maps = broadcastMapper.selectMaps(queryWrapper);
        return JsonUtils.jsonToList(maps, BroadcastCount.class);
    }

    /**
     * 分组统计
     *
     * @param groupIds
     * @return
     */
    @Override
    public List<BroadcastCount> countByGroup(List<Long> groupIds) {
        QueryWrapper<BroadcastDO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .in(Collections3.isNotEmpty(groupIds), "group_id", groupIds)
                .select("count(1) as num, group_id as id")
                .groupBy(" group_id");
        List<Map<String, Object>> maps = broadcastMapper.selectMaps(queryWrapper);
        return JsonUtils.jsonToList(maps, BroadcastCount.class);
    }

    /**
     * 按主播统计
     *
     * @param anchorIds
     * @return
     */
    @Override
    public List<BroadcastCount> countByAnchor(List<Long> anchorIds) {
        QueryWrapper<BroadcastDO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .in(Collections3.isNotEmpty(anchorIds), "author_id", anchorIds)
                .select("count(1) as num, author_id as id")
                .groupBy(" author_id");
        List<Map<String, Object>> maps = broadcastMapper.selectMaps(queryWrapper);
        return JsonUtils.jsonToList(maps, BroadcastCount.class);
    }
}
